package controllers

import (
	"github.com/astaxie/beego"
	"beeblog/models"
	"strconv"
)

type CategoryController struct {
	beego.Controller
}

func (this *CategoryController) Get() {
	flag := checkAccount(this.Ctx)
	this.Data["IsLogin"] = flag
	op := this.Input().Get("op")
	switch  op {

	case "add":
		if !flag{
			this.Redirect("/login",301)
			return
		}
		name := this.Input().Get("name")
		if len(name) == 0{
			break
		}

		err := models.AddCategory(name)
		if err!=nil{
			beego.Error(err)
		}
		this.Redirect("/category",301)
		return
	case "del":
		if !flag{
			this.Redirect("/login",301)
			return
		}
		id := this.Input().Get("id")
		if len(id) == 0{
			break
		}
		int_id, err:= strconv.Atoi(id)
		if err !=nil{
			break
		}
		err = models.DelCategory(int64(int_id))
		if err != nil{
			beego.Error(err)
		}
		this.Redirect("/category",301)
		return
	}

	this.Data["IsCategory"] = true
	var err error
	this.Data["Categories"] ,err = models.GetAllCategories()
	if err!=nil{
		beego.Error("helloword=====================",err)
	}

	this.TplName = "category.html"
}